package com.hcs.chenyu.chenyuusedmarket;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.hcs.chenyu.R;

public class vegetables extends AppCompatActivity {

    //需要适配的数据
    private String[] titles = { "卷心菜", "胡萝卜", "大蒜", "土豆", "西红柿",
            "黄瓜", "更多"};
    private String[] prices = { "1元以上", "1元以上", "1元以上", "1元以上", "1元以上",
            "1元以上", "更多用品等你来买"};
    //图片集合
    private int[] icons = {R.drawable.cabbage,R.drawable.carrot,R.drawable.garlic,
            R.drawable.potato,R.drawable.tomato,R.drawable.cucumber,R.drawable.gengduo};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vegetables);

        ListView mListViews = findViewById(R.id.list_viewvegetable);


        myBaseAdapter adapter = new myBaseAdapter();
        mListViews.setAdapter(adapter);


        mListViews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                System.out.println(position);
                switch (position){
                    case 0:
                        Intent intent = new Intent(vegetables.this, cabbage.class);
                        startActivity(intent);
                        break;
                    case 1:
                        Intent intent1 = new Intent(vegetables.this, carrot.class);
                        startActivity(intent1);
                        break;
                    case 2:
                        Intent intent2 = new Intent(vegetables.this, garlic.class);
                        startActivity(intent2);
                        break;
                    case 3:
                        Intent intent3 = new Intent(vegetables.this, potato.class);
                        startActivity(intent3);
                        break;
                    case 4:
                        Intent intent4 = new Intent(vegetables.this, tomato.class);
                        startActivity(intent4);
                        break;
                    case 5:
                        Intent intent5 = new Intent(vegetables.this, cucumber.class);
                        startActivity(intent5);
                        break;
                    case 6:
                        Intent intent6 = new Intent(vegetables.this,vegetablemore.class);
                        startActivity(intent6);
                        break;

                }

            }
        });
    }

    class myBaseAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Object getItem(int position) {
            return titles[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = View.inflate(vegetables.this, R.layout.listviewbujv, null);
            ImageView iv = view.findViewById(R.id.iv);
            TextView title = view.findViewById(R.id.title);
            TextView price = view.findViewById(R.id.price);

            iv.setBackgroundResource(icons[position]);
            title.setText(titles[position]);
            price.setText(prices[position]);

            return view;
        }
    }


}
